package com.xavier.controller;

import com.xavier.dto.UserDTO;
import com.xavier.dto.UserQueryDTO;
import com.xavier.result.Result;
import com.xavier.service.IUserService;
import com.xavier.vo.QueryResultVO;
import com.xavier.vo.UserDetailVO;
import com.xavier.vo.UserPageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 请求处理层-用户管理类
 *
 * @author Xavier
 * @version 1.0
 */
@Slf4j
@RequestMapping("user")
@RestController
@Tag(name = "用户管理接口")
public class UserController extends CommonController {

    @Resource
    private IUserService userService;

    @Operation(summary = "新增用户", description = "新增用户")
    @PutMapping("add")
    public Result add(@Validated @RequestBody UserDTO dto) {
        userService.createUser(dto);
        return Result.success();
    }

    @Operation(summary = "编辑用户", description = "编辑用户")
    @PutMapping("update")
    public Result update(@Validated @RequestBody UserDTO dto) {
        userService.updateUser(dto);
        return Result.success();
    }

    @Operation(summary = "删除用户", description = "删除用户")
    @DeleteMapping("delete/{id}")
    @Parameter(name = "id", description = "id", required = true)
    public Result<Integer> delete(@PathVariable Long id) {
        userService.deleteUser(id);
        return Result.success();
    }

    @Operation(summary = "获取用户信息", description = "根据用户id获取用户信息")
    @GetMapping("get/{id}")
    @Parameter(name = "id", description = "id", required = true)
    public Result<UserDetailVO> get(@PathVariable Long id) {
        return Result.success(userService.getUser(id));
    }

    @Operation(summary = "分页获取用户列表信息", description = "分页获取用户列表信息")
    @PostMapping("query")
    public Result<QueryResultVO<UserPageVO>> queryUser(@Validated @RequestBody UserQueryDTO dto) {
        return Result.success(userService.queryUser(dto));
    }

}
